import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import axios from 'axios'

// 忽略 ResizeObserver 错误
const ignoreResizeObserverError = () => {
  window.addEventListener('error', (e) => {
    if (e.message === 'ResizeObserver loop completed with undelivered notifications.') {
      e.stopImmediatePropagation()
    }
  })

  window.addEventListener('unhandledrejection', (e) => {
    if (e.reason && e.reason.message === 'ResizeObserver loop completed with undelivered notifications.') {
      e.preventDefault()
    }
  })
}

ignoreResizeObserverError()

const app = createApp(App)

// 配置axios
axios.defaults.baseURL = '/api'  // 修改为相对路径，配合vue.config.js的代理设置
axios.interceptors.request.use(config => {
  const token = localStorage.getItem('token')
  if (token) {
    config.headers.Authorization = `Bearer ${token}`
  }
  return config
})

// 注册所有Element Plus图标
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
  }

app.use(router)
app.use(ElementPlus)

// 全局错误处理
app.config.errorHandler = (err, vm, info) => {
  if (err.message === 'ResizeObserver loop completed with undelivered notifications.') {
    return
  }
  console.error('Vue Error:', err, info)
}

app.mount('#app')
